package com.bm.bookmanagement.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bm.bookmanagement.entity.UserInfo;
import com.bm.bookmanagement.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;

@Controller
@Slf4j
public class LoginController {
    @Autowired
    private UserService userService;

    /**
     * redis操作
     */
    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping({"/"})
    public String portal() {
        return "login";
    }

    @RequestMapping("/index")
    public String index(int id, Model model, HttpServletRequest request) {
        UserInfo user = userService.getById(id);
        model.addAttribute("user", user);
//        request.getSession().setAttribute("LoginSuccess",user);
        return user.getFlag() == 0 ? "admin_index" : "reader_index";
    }

    @RequestMapping({"/login"})
    public String login(UserInfo user, Model model, HttpServletRequest request) {
        LambdaQueryWrapper<UserInfo> queryWrapper1 = new LambdaQueryWrapper();
        queryWrapper1.eq(user.getUsername() != null, UserInfo::getUsername, user.getUsername());
        UserInfo user1 = this.userService.getOne(queryWrapper1);
        if (user1 != null) {

            String lastLoginTime = redisTemplate.opsForValue().get("user_" + user1.getId());
            if (StringUtils.isEmpty(lastLoginTime)) {
                log.info("账号:{} 首次登录", user1.getName());
            } else {
                long l = Long.parseLong(lastLoginTime);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String format = sdf.format(l);
                log.info("账号:{} 上一次登录时间:{}", user1.getName(), format);
            }

            redisTemplate.opsForValue().set("user_" + user1.getId(), Long.toString(System.currentTimeMillis()));

            if (user1.getPassword().equals(user.getPassword()) && user1.getFlag() == user.getFlag()) {
                model.addAttribute("user", user1);
                request.getSession().setAttribute("LoginSuccess", user1);
                return user.getFlag() == 0 ? "admin_index" : "reader_index";
            } else {
                model.addAttribute("msg", "密码或身份错误！");
                return "forward:/";
            }
        } else {
            model.addAttribute("msg", "账号不存在！");
            return "forward:/";
        }
    }

    //跳转到锁屏
    @RequestMapping("/toLock")
    public String toLock(int id, Model model) {
        UserInfo user = userService.getById(id);
        model.addAttribute("user", user);
        return "lock";
    }

    //解锁
    @RequestMapping("/lock")
    public String lock(int id, String password, Model model) {
        UserInfo user = userService.getById(id);
        model.addAttribute("user", user);
        if (user.getPassword().equals(password))
            return user.getFlag() == 0 ? "admin_index" : "reader_index";
        else {
            model.addAttribute("msg", "密码输入错误，请重试!");
            return "lock";
        }
    }
}
